package com.zztl.infiniteBand.ui.view.dialog

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow

import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BaseActivity


/**
 * Created by lsm on 2016/9/9.
 */

class SharePop : PopupWindow {
    private var mDismissListener: DismissListener? = null

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    class Builder(private val mContext: BaseActivity) {
        private var canCancel: Boolean = false
        private var mClickListener: ClickListener? = null
        private var mDismissListener: DismissListener? = null

        fun setCanCancel(canCancel: Boolean): Builder {
            this.canCancel = canCancel
            return this
        }

        fun setClickListener(clickListener: ClickListener): Builder {
            this.mClickListener = clickListener
            return this
        }

        fun setDismissListener(dismissListener: DismissListener): Builder {
            this.mDismissListener = dismissListener
            return this
        }


        fun build(): SharePop {
            val inflater = LayoutInflater.from(mContext)
            val view = inflater.inflate(R.layout.pop_share, null)
            val sharePop = SharePop(mContext)
            sharePop.contentView = view
            initClick(view)
            sharePop.width = LinearLayout.LayoutParams.MATCH_PARENT
            // 设置SelectPicPopupWindow弹出窗体的高
            sharePop.height = LinearLayout.LayoutParams.WRAP_CONTENT
            if (canCancel) {
                sharePop.isFocusable = true

                sharePop.isOutsideTouchable = true
            } else {
                // 设置SelectPicPopupWindow弹出窗体可点击
                sharePop.isFocusable = false
                sharePop.isOutsideTouchable = false
            }
            if (null != mDismissListener) {
                sharePop.setDismissListener(mDismissListener!!)
            }
            // 刷新状态
            sharePop.update()
            sharePop.animationStyle = android.R.style.Animation_Dialog
            if (!sharePop.isShowing) {
                sharePop.showAtLocation(mContext.window.decorView, Gravity.BOTTOM, 0, 0)
            }
            return sharePop
        }

        private fun initClick(view: View) {
            view.findViewById<View>(R.id.img_share).setOnClickListener {
                if (mClickListener != null) {
                    mClickListener!!.imgShare()
                }
            }
            view.findViewById<View>(R.id.page_share).setOnClickListener {
                if (mClickListener != null) {
                    mClickListener!!.share()
                }
            }
            view.findViewById<View>(R.id.link_share).setOnClickListener {
                if (mClickListener != null) {
                    mClickListener!!.textShare()
                }
            }
        }
    }

    private fun setDismissListener(dismissListener: DismissListener) {
        this.mDismissListener = dismissListener
    }


    interface ClickListener {
        fun imgShare()

        fun share()

        fun textShare()
    }


    /*   */

    /**
     * 显示popupWindow
     *//*
    public void showPopupWindow() {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAtLocation(context.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
        }
    }*/
    override fun dismiss() {
        super.dismiss()
        if (null != mDismissListener) {
            mDismissListener!!.dismiss()
        }
    }

    companion object {
        private val TAG = SharePop::class.java.simpleName
    }
}